// components/show/show.js
const computedBehavior = require("miniprogram-computed").behavior;
Component({
  behaviors: [computedBehavior],
  /**
   * 组件的属性列表
   */
  properties: {

  },
  /**
   * 组件的初始数据
   */
  data: {
    list: [],
    totalPrice:0,
  },
  //计算属性
  computed: {
    totalPrice(data){
      let result = data.list.reduce((total,item)=>{
          if(item.checked){
            return total+= item.minPrice*item.count
          }
          return total;
      },0)
      // console.log(result,"$$$")
      return result*100;
    },
    // 计算是否全选
    totalChecked(data) {
      return data.list.every(item => {
        return item.checked == true
      })
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    //去订单
    goOrder(){
      wx.navigateTo({
        url: '../../components/order/order',
      })
    },
    countChange(e){
      // console.log(e);
      let index = e.currentTarget.dataset.index
      this.data.list[index].count=e.detail
      this.setData({
        list:this.data.list
      })
      wx.setStorageSync('cart',  this.data.list)
    },
    //删除
    del(e){
      // console.log(e);
      let {index} = e.currentTarget.dataset
      this.data.list.splice(index,1)
      wx.setStorageSync('cart', this.data.list)
      this.setData({
        list:this.data.list
      })
    },
    //单选
    singleCheck(e) {
      let index = e.target.dataset.index;
      this.data.list[index].checked = !this.data.list[index].checked;
      wx.setStorageSync('cart', this.data.list)
      this.setData({
        list: this.data.list,
      });
    },
    //全选
    allCheck() {
      let result = !this.data.totalChecked
      //改变list数据,在逻辑层进行改变
      this.data.list.forEach((item) => {
        item.checked = result
      });
      wx.setStorageSync('cart', this.data.list)
      //渲染到页面
      this.setData({
        list: this.data.list
      })
    },
  },
  pageLifetimes:{
        show(){
          this.data.list= wx.getStorageSync('cart')
          this.setData({
            list:this.data.list
          })
        }
      }
})